#include "mainwindow.h"
#include "ui_mainwindow.h"


#include <QAction>
#include <QMenuBar>
#include <QMessageBox>
#include <QStatusBar>
#include <QToolBar>
#include <QTextEdit>
#include <QFileDialog>
#include <QTextStream>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    openAction = new QAction(QIcon(":/images/file-open"), tr("&Open..."), this);
    openAction->setShortcuts(QKeySequence::Open);
    openAction->setStatusTip(tr("Open an existing file"));

    saveAction = new QAction(QIcon(":/images/file-save"), tr("&Save..."), this);
    saveAction->setShortcuts(QKeySequence::Save);
    saveAction->setStatusTip(tr("Save a new file"));

    QMenu *file = menuBar()->addMenu(tr("&File"));
    file->addAction(openAction);
    file->addAction(saveAction);

    QToolBar *toolBar = addToolBar(tr("&File"));
    toolBar->addAction(openAction);
    toolBar->addAction(saveAction);

    textEdit = new QTextEdit(this);
    setCentralWidget(textEdit);


    /// !!!Qt5
    connect(openAction, &QAction::triggered, this, &MainWindow::openFile);
    connect(saveAction, &QAction::triggered, this, &MainWindow::saveFile);

//    /// !!!Qt4
//    connect(openAction, SIGNAL(triggered()), this, SLOT(openFile()));
//    connect(saveAction, SIGNAL(triggered()), this, SLOT(saveFile()));
}


void MainWindow::openFile(){
    QString path = QFileDialog::getOpenFileName(this,
                                                   tr("Open File"),
                                                   ".",
                                                   tr("Text Files(*.txt)"));
       if(!path.isEmpty()) {
           QFile file(path);
           if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
               QMessageBox::warning(this, tr("Read File"),
                                    tr("Cannot open file:\n%1").arg(path));
               return;
           }
           QTextStream in(&file);
           textEdit->setText(in.readAll());
           file.close();
       } else {
           QMessageBox::warning(this, tr("Path"),
                                tr("You did not select any file."));
       }
}


void MainWindow::saveFile()
{
    QString path = QFileDialog::getSaveFileName(this,
                                                tr("Open File"),
                                                ".",
                                                tr("Text Files(*.txt)"));
    if(!path.isEmpty()) {
        QFile file(path);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            QMessageBox::warning(this, tr("Write File"),
                                       tr("Cannot open file:\n%1").arg(path));
            return;
        }
        QTextStream out(&file);
        out << textEdit->toPlainText();
        file.close();
    } else {
        QMessageBox::warning(this, tr("Path"),
                             tr("You did not select any file."));
    }
}


MainWindow::~MainWindow()
{
    delete ui;
}

